<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Journey_cycle extends CI_Controller {

	function journey_cycle()
	{
		parent::__construct();
		$this->load->library('session');
		$this->load->library('form_validation');
		$this->load->library('encrypt');
		$this->load->model('mmaster');
                $this->load->model('mservices');
		$this->load->model('mterritory');
		$this->load->model('mjourney');
		$this->load->model('musers');
		$this->is_logged_in();
		#$this->mjadwal->saveLog();
	}
        
        function is_logged_in()
	{
		$is_logged_in = $this->session->userdata('is_logged_in');
		if(!isset($is_logged_in) || $is_logged_in != true)
		{
			redirect(base_url() .'index.php/login');
		}		
	}
        
	##SFA
        function index($sfa="",$bulan=""){
                $data['template']       = "shell/smooth";
		$data['main_view']      = "f-journey-cycle";
		
		#if($bulan=="") $bulan = date('m');
                $data['sfa']            = $sfa;
                $data['bulan']          = $bulan;
		$data['tahun']          = date("Y");
		$data['getSFA']		= $this->mjourney->getSFA();
		
		$data['getJourney'] = array();
		$data['getHoliday'] = array();			
			
		// $data['cluster'] = $this->mglobal->showdata("territory_name","t_mtr_territory",array("user_id"=>$this->session->userdata('username')),"dblokal");
		//$userLogin = $this->session->userdata('username');
		//$clusterId =  $this->dblokal->query("select f_get_cluster('$userLogin')")->row;
		//$pid = $this->mglobal->showdata("parent_id","t_mtr_territory",array("territory_id"=>$clusterId),"dblokal");
		$teritori = explode('>', $this->mglobal->getTerritoryPath());

		$data['cluster'] = $teritori[0];
		$data['region'] = $teritori[1];
		//$data['region'] = $this->mglobal->showdata("territory_name","t_mtr_territory",array("territory_id"=>$pid),"dblokal");
		if ($teritori[1]) {
			$this->load->view($data['template'],$data);
		}
		else {
			redirect("index.php/cluster");
		}
        }
	
	function smartfren_ambassador($sfa="",$bulan=""){
		$this->index($sfa,$bulan);
	}
	
	function getParent(){
		
		$sfa = $this->input->post('sfa');
		$ter_id = $this->mglobal->showdata("territory_id","t_mtr_user_territory",array("user_id"=>$sfa),"dblokal");
		$par_id = ""; 		$reg_id = "";
		$region = ""; 	$cluster="";
		if($ter_id <> ""){
			$par_id = $this->mglobal->showdata("parent_id","t_mtr_territory",array("territory_id"=>$ter_id),"dblokal");
			$reg_id = $this->mglobal->showdata("parent_id","t_mtr_territory",array("territory_id"=>$par_id),"dblokal");
			$cluster = $this->mglobal->showdata("territory_name","t_mtr_territory",array("territory_id"=>$par_id),"dblokal");
			$region = $this->mglobal->showdata("territory_name","t_mtr_territory",array("territory_id"=>$reg_id),"dblokal");
		}
			
		echo json_encode(array(
			'status'=>'OK',
			'region' => $region,
			'cluster' => $cluster
		));
		return; 
	}
	
	function get_jc_sfa(){
		$data['bulan'] 		= $this->input->post('bulan');
		$data['tahun'] 		= date('Y');
		$data['sfa'] 		= $this->input->post('sfa');
		$data['cari'] 		= $this->input->post('cari');
		$data['page']  = "";
		$yyyymm = $data['tahun'].$data['bulan'];
		
		if($data['sfa'] <>"" && $data['bulan'] <>"" ){
			
			$ada = $this->mglobal->showdata('user_id','t_mtr_user',array('user_id'=>$data['sfa']),'dblokal');
			if($ada == $data['sfa']){ //jika ada, maka data valid & boleh dibuat tabelnya
				$this->mjourney->createTabel("t_jc_".$data['sfa']);
			}
            
            $getJourney 	= $this->mjourney->getJourney($data['sfa'],$data['bulan'],$data['tahun'],"");
			if(count($getJourney)==0){
				#input data journey
				$this->mjourney->insertJourney($data['sfa'],$data['bulan'],$data['tahun']);
			}
			// $data['jumlah'] 	  = $this->mjourney->getJourney($data['sfa'],$data['bulan'],$data['tahun'],"count",$data['cari']);
			// yaddis@nutech --- 09/05/2012
			$jumlah = $this->mjourney->sfa_outlets($data["sfa"], $yyyymm);
			// print_r($data["jumlah"]); 
			$data['getJourney'] = $this->mjourney->getJourney($data['sfa'],$data['bulan'],$data['tahun'],"1",$data['cari']);
			$data['getHoliday'] = $this->mjourney->getHoliday($data['bulan'],date("Y"));
			
			$this->load->library('pagination');
			$config['uri_segment'] = 3;
			$config['base_url'] = '#';
			// $config['total_rows'] = count($data['jumlah']);
			$config['total_rows'] = intval($jumlah["count"]);
			$config['per_page'] = 15;
			// $config['num_links'] = 8;
			
			$config['first_link'] = FALSE;
			$config['last_link'] = FALSE;
			$config['next_link'] = FALSE;
			$config['prev_link'] = FALSE;
			
			// $config['use_page_numbers'] = FALSE;
			// $config['page_query_string'] = FALSE;
			
			$config['full_tag_open'] = '<ul class="pager">';
			$config['full_tag_close'] = '</ul>';
			
			$config['num_tag_open'] = '<li id="hallo">';
			$config['num_tag_close'] = '</li>';
			
			$config['cur_tag_open'] = '<li class="current">';
			$config['cur_tag_close'] = '</li>';
			
			$this->pagination->initialize($config);
			
			$hasil = $this->load->view('f-jc-sfa',$data,TRUE);
			echo json_encode(array(
				'status'=>'OK',
				'jumlah'=>$jumlah["count"],
				'datatabel' => $hasil
			));
			return; 
		}
	}
	
	function get_jc_sfa_pagination(){
		$data['bulan'] 		= $this->input->post('bulan');
		$data['tahun'] 		= date('Y');
		$data['sfa'] 		= $this->input->post('sfa');		
		$data['page'] 		= $this->input->post('page');
		$data['cari'] 		= $this->input->post('cari');
		
		if($data['sfa'] <>"" && $data['bulan'] <>"" ){
			$data['getJourney'] 	= $this->mjourney->getJourney($data['sfa'],$data['bulan'],$data['tahun'],$data['page'],$data['cari']);
			$data['getHoliday']	= $this->mjourney->getHoliday($data['bulan'],date("Y"));
			//$data['jumlah'] 	= $this->mjourney->getJourney($data['sfa'],$data['bulan'],$data['tahun'],"count");
			
			$this->load->library('pagination');
			
			$config['base_url'] = '#';
			$config['total_rows'] = 700;//count($data['jumlah']);
			$config['per_page'] = 15;
			$config['num_links'] = 8;
			$config['use_page_number'] = TRUE;
			$config['uri_segment'] = 3;
			
			$config['first_link'] = FALSE;
			$config['last_link'] = FALSE;
			$config['next_link'] = FALSE;
			$config['prev_link'] = FALSE;
			
			$config['full_tag_open'] = '<ul class="pager">';
			$config['full_tag_close'] = '</ul>';
			
			$config['num_tag_open'] = '<li id="hallo">';
			$config['num_tag_close'] = '</li>';
			
			$config['cur_tag_open'] = '<li class="current">';
			$config['cur_tag_close'] = '</li>';
			
			$this->pagination->initialize($config);
			
			$hasil = $this->load->view('f-jc-sfa',$data,TRUE);
			echo json_encode(array(
				"status"=>"OK",
				"datatabel" => $hasil
			));
			return; 
		}
	}
	
	########DS
	function direct_sales($sfa="",$minggu=""){
                $data['template']       = "shell/smooth";
		$data['main_view']      = "f-journey-cycle-direct-sales";
		
		#if($bulan=="") $bulan = date('m');
                $data['sfa']            = $sfa;
                $data['minggu']          = $minggu;
		$data['tahun']          = date("Y");
        
		$data['getDS']		= $this->mjourney->getDS();
        
		$data['getAgent']	= $this->musers->getUser();//$this->mterritory->getTerritoryByType(5);
		$data['getJourney'] = array();
		$data['getHoliday'] = array();
        
        $teritori = explode('>', $this->mglobal->getTerritoryPath());

		$data['cluster'] = $teritori[0];
		$data['region'] = $teritori[1];

		$data['agentstatus'] = $this->mservices->getValueFromTable("v_combo_agent_status");
		if($this->input->post("submit")){
			$c_agent = $this->input->post("c_agent");
			if(count($c_agent)>0 && $sfa <> "" && $minggu <>""){
				$this->mjourney->insertJourney_ds($c_agent,$sfa,$minggu,date("Y"));
			}
		}
		
		$this->load->view($data['template'],$data);
        }
	
	function get_jc_ds(){
		$data['minggu'] 	= $this->input->post('minggu');
		$data['tahun'] 		= date('Y');
		$data['sfa'] 		= $this->input->post('sfa');
		$data['tipe_penjualan'] 		= $this->input->post('tipe_penjualan');
		
		if($data['sfa'] <>"" && $data['minggu'] <>"" ){
		  
            $ada = $this->mglobal->showdata('user_id','t_mtr_user',array('user_id'=>$data['sfa']),'dblokal');
			if($ada == $data['sfa']){
				$this->mjourney->createTabelDS("t_jc_".$data['sfa']);
			}
            
			$data['getJourney'] 	= $this->mjourney->getJourney_ds($data['sfa'],$data['minggu'],$data['tahun']);
			$data['getHoliday']	= "";
			
			$hasil = $this->load->view('f-jc-ds',$data,TRUE);
			$agent = "";
			
			$sStartDate = $this->mglobal->week_start_date($data['minggu']-1, $data['tahun']); 
			for($t=1;$t<=7;$t++){
					$sDate   = date('Y-m-d', strtotime('+'. ($t-1) .' days', strtotime($sStartDate)));
					if($sDate >= date('Y-m-d')) $agent .= "show";
			}
			if($agent <> '') $agent='show';
			echo json_encode(array(
				'status'=>'OK',
				'jumlah'=>count($data['getJourney']),
				'datatabel' => $hasil,
				'agent' => $agent
			));
			return; 
		}
	}
	
	function get_jc_ds_status(){
		$status 		= $this->input->post('status');
		
		$hasil = '<select name="sfa" id="sfa" class="d_jc_ds">';
		$hasil .= '<option value="">-Choice-</option>';
		
		if($status <>"" ){
			##ambil ds
			$getDS		= $this->mjourney->getDS($status);
			foreach($getDS as $r){
					$hasil .='<option value="'. $r->user_id .'">'. $r->user_name .'</option>';
			}
		}		
		$hasil .= '</select>';
		
		echo json_encode(array(
			'status'=>'OK',
			'hasil'=> $hasil
		));
		return; 
	}
	
	function add_agent_ds(){
		$data['minggu'] 	= $this->input->post('minggu');
		$data['tahun'] 		= date('Y');
		$data['sfa'] 		= $this->input->post('sfa');
		$c_agent 		= $this->input->post("c_agent");
		
		if(count($c_agent) > 0 && $data['sfa'] <> "" && $data['minggu'] <>""){
			
			$this->mjourney->insertJourney_ds($c_agent,$data['sfa'],$data['minggu'],date("Y"));
			$data['getJourney'] 	= $this->mjourney->getJourney_ds($data['sfa'],$data['minggu'],$data['tahun']);
			$hasil = $this->load->view('f-jc-ds',$data,TRUE);
			echo json_encode(array(
				'status'=>'OK',
				'jumlah'=>count($data['getJourney']),
				'datatabel' => $hasil
			));
			return;
		}		
	}
	
	function saveJC(){
		$nilai 	= $this->input->post("nilai");
		$sfa 	= $this->input->post("sfa"); //$this->session->userdata("username");
		$outlet = $this->input->post("outlet");
		$tgl 	= $this->input->post("tgl");
		$bulan 	= $this->input->post("bulan");
		$kunjunganoutlet = $this->input->post('kunjunganoutlet');
		$jmlkunjungan = $this->input->post('jmlkunjungan');
		$outlet_class = 'A';//$this->input->post("outlet_class");
		
		#hitung nilai maksimum
		$max = $this->mglobal->showdata('lookup_value','t_mtr_lookup',array('lookup_key'=>'jc_sfa_max','lookup_desc'=>$outlet_class),'dblokal');
		$min = $this->mglobal->showdata('lookup_value','t_mtr_lookup',array('lookup_key'=>'jc_sfa_min','lookup_desc'=>$outlet_class),'dblokal');
		$kunjungan = $this->mjourney->getJumlahKunjungan($sfa,$outlet,$bulan);
		if($kunjungan >= $max && $nilai == false){
			echo json_encode(array(
				'status'=>'CANCEL',
				'pesan' => 'Batas Kunjungan Hanya '. $kunjungan .' kali.'
			));
			return; 
		}else{
			if($nilai == "true"){
				$nilai = 1;
				$jmlkunjungan ++;
				$kunjunganoutlet ++;
			}else{
				$nilai = 0;
				$jmlkunjungan --;
				$kunjunganoutlet --;
			}
			$this->mjourney->saveJourney($sfa,$outlet,$tgl,$bulan,$nilai);
			$kunjungan = $this->mjourney->getJumlahKunjungan($sfa,$outlet,$bulan);
			$kunjunganpertanggal = $this->mjourney->getTotalKunjungan($sfa, $tgl, substr($bulan,4,2), substr($bulan,0,4));
			echo json_encode(array(
				'status'=>'OK',
				'pesan' => 'Success to Maintenance Journey Cycle',
				'cbackground' => ($kunjungan <= $min ? "yellow":"white"),
				'kunjungan' => $kunjunganpertanggal,
				'kunjunganoutlet'=>$kunjunganoutlet,
				'totalkunjungan'=>$jmlkunjungan
			));
			return; 
		}
	}
	
	function saveJC_ds(){
		$nilai = $this->input->post("nilai");
		$sfa = $this->input->post("sfa"); //$this->session->userdata("username");
		$outlet = $this->input->post("outlet");
		$tgl = $this->input->post("tgl");
		$minggu = $this->input->post("minggu");
		
		if($nilai == "true"){$nilai = 1;}else{$nilai = 0;}
		$this->mjourney->saveJourney_ds($sfa,$outlet,$tgl,$minggu,$nilai);
		echo json_encode(array(
			'status'=>'OK',
			'pesan' => 'Success to Maintenance Journey Cycle'
		));
		return; 
	}
	
	function saveJC_sgs(){
		$nilai 	= $this->input->post("nilai");
		$sfa 	= $this->input->post("sfa"); //$this->session->userdata("username");
		$outlet = $this->input->post("outlet");
		$tgl 	= $this->input->post("tgl");
		$bulan 	= $this->input->post("bulan");
		$kunjunganoutlet = $this->input->post('kunjunganoutlet');
		$jmlkunjungan = $this->input->post('jmlkunjungan');
		$outlet_class = 'A';//$this->input->post("outlet_class");
		
		#hitung nilai maksimum
		$max = $this->mglobal->showdata('lookup_value','t_mtr_lookup',array('lookup_key'=>'jc_sfa_max','lookup_desc'=>$outlet_class),'dblokal');
		$min = $this->mglobal->showdata('lookup_value','t_mtr_lookup',array('lookup_key'=>'jc_sfa_min','lookup_desc'=>$outlet_class),'dblokal');
		$kunjungan = $this->mjourney->getJumlahKunjunganSGS($sfa,$bulan);
		if($kunjungan >= $max && $nilai == false){
			echo json_encode(array(
				'status'=>'CANCEL',
				'pesan' => 'Batas Kunjungan Hanya '. $kunjungan .' kali.'
			));
			return; 
		}else{
			if($nilai == "true"){
				$nilai = 1;
				$jmlkunjungan ++;
				$kunjunganoutlet ++;
			}else{
				$nilai = 0;
				$jmlkunjungan --;
				$kunjunganoutlet --;
			}
			$this->mjourney->saveJourney_sgs($sfa,$outlet,$tgl,$bulan,$nilai);
			$kunjungan = $this->mjourney->getJumlahKunjunganSGS($sfa,$bulan);
			$kunjunganpertanggal = $this->mjourney->getTotalKunjunganSGS($sfa, $tgl, substr($bulan,4,2), substr($bulan,0,4));
			echo json_encode(array(
				'status'=>'OK',
				'pesan' => 'Success to Maintenance Journey Cycle',
				'cbackground' => ($kunjungan <= $min ? "yellow":"white"),
				'kunjungan' => $kunjunganpertanggal,
				'kunjunganoutlet'=>$kunjunganoutlet,
				'totalkunjungan'=>$jmlkunjungan
			));
			return; 
		}
	}
	
	function copyJC(){
		$sfa = $this->input->post("sfa"); //$this->session->userdata("username");
		$bulan = date("m");
		$tahun = date("Y");
		$bulan_asal = $this->input->post("bulan");
		$tahun_asal = $this->input->post("tahun");
		
		//periksa dulu, sudah ada yang diceklis/blm
		if ($this->mjourney->cekAdaJourney($sfa,$bulan,$tahun) == TRUE){
			$this->mjourney->copyJourney($sfa,$bulan_asal,$tahun_asal,$bulan,$tahun);
			
			echo json_encode(array(
				'status'=>'OK',
				'pesan' => 'Success to Copy Journey Cycle'
			));
			return;
		}else{
			echo json_encode(array(
				'status'=>'OK',
				'pesan' => 'Tidak dapat Journey Cycle karena di bulan '.date('F Y').' sudah ada perjalanan.'
			));
			return;
		}
	}
	
	function copySGS(){
		$sfa = $this->input->post("sfa"); //$this->session->userdata("username");
		$bulan = date("m");
		$tahun = date("Y");
		$bulan_asal = $this->input->post("bulan");
		$tahun_asal = $this->input->post("tahun");
		
		//periksa dulu, sudah ada yang diceklis/blm
		if ($this->mjourney->getJumlahKunjunganSGS($sfa,$tahun.$bulan) == 0){
			$this->mjourney->copyJourneySGS($sfa,$bulan_asal,$tahun_asal,$bulan,$tahun);
			
			echo json_encode(array(
				'status'=>'OK',
				'pesan' => 'Success to Copy Journey Cycle'
			));
			return;
		}else{
			echo json_encode(array(
				'status'=>'OK',
				'pesan' => 'Tidak dapat Journey Cycle karena di bulan '.date('F Y').' sudah ada perjalanan.'
			));
			return;
		}
	}
	
	####################
	### Get JSON DATA
	function week_json(){
	    echo($this->mjourney->getWeek_json());
	}
	function agent_json($id="",$agentstatus="",$sfa=""){
	    if($id <> "" && $agentstatus <> "" && $sfa <> ""){
			echo($this->mjourney->getAgent_json($id,$agentstatus,$sfa));
		}
	}
	
	
	//////////////////////////////////////////
	// JC SGS ////////////////////////////////
	//////////////////////////////////////////
	function sgs($sfa="",$minggu=""){
        $data['template']       = "shell/smooth";
		$data['main_view']      = "f-journey-cycle-sgs";
		
		#if($bulan=="") $bulan = date('m');
        $data['sfa']            = $sfa;
        $data['minggu']          = $minggu;
		$data['tahun']          = date("Y");
		$data['getDS']		= $this->mjourney->getDS();
		$data['getAgent']	= $this->musers->getUser();;//$this->mterritory->getTerritoryByType(5);
		
		$data['getJourney'] = array();
		$data['getHoliday'] = array();
		
		// $pid = $this->mglobal->showdata("parent_id","t_mtr_territory",array("user_id"=>$this->session->userdata('username')),"dblokal");
		// $data['cluster'] = $this->mglobal->showdata("territory_name","t_mtr_territory",array("user_id"=>$this->session->userdata('username')),"dblokal");
		// $data['region'] = $this->mglobal->showdata("territory_name","t_mtr_territory",array("territory_id"=>$pid),"dblokal");
		
		// yaddis@nutech-integrasi.com --- 07/31/201
		$teritori = explode('>', $this->mglobal->getTerritoryPath());
		$data['cluster'] = $teritori[0];
		$data['region'] = $teritori[1];

		$data['agentstatus'] = $this->mservices->getValueFromTable("v_combo_agent_status");
		if($this->input->post("submit")){
			$c_agent = $this->input->post("c_agent");
			if(count($c_agent)>0 && $sfa <> "" && $minggu <>""){
				$this->mjourney->insertJourney_ds($c_agent,$sfa,$minggu,date("Y"));
			}
		}
		
		if ($teritori[1]) {
			$this->load->view($data['template'],$data);
		}
		else {
			redirect("index.php/cluster");
		}
		
    }
	
	function get_jc_sgs(){
		$data['bulan'] 	= $this->input->post('bulan');
		$data['tahun'] 		= date('Y');
		$data['sfa'] 		= $this->input->post('sfa');
		$data['tipe_penjualan'] 		= $this->input->post('tipe_penjualan');
		$data['cari'] 		= "";
		$data['page'] 		= "";
		
		$usergroup = $this->mglobal->getGroupId();;
		$nmtbl = "t_jc_".$usergroup; // ambil group 
		if($data['sfa'] <>"" && $data['bulan'] <>"" ){
			$this->mjourney->createTabel( $nmtbl );
            
            $data['jumlah'] 	= $this->mjourney->getJourneySGS($data['sfa'],$data['bulan'],$data['tahun'],"",$data['cari']);
			$data['getJourney'] 	= $this->mjourney->getJourneySGS($data['sfa'],$data['bulan'],$data['tahun'],"1",$data['cari']);
			$data['getHoliday']	= $this->mjourney->getHoliday($data['bulan'],date("Y"));
			
		    /*
            $sStartDate = $this->mglobal->week_start_date($data['minggu']-1, $data['tahun']); */
			
			$this->load->library('pagination');
			$config['uri_segment'] = 3;
			$config['base_url'] = '#';
			$config['total_rows'] = count($data['jumlah']);
			$config['per_page'] = 15;
			$config['num_links'] = 8;
			
			$config['first_link'] = FALSE;
			$config['last_link'] = FALSE;
			$config['next_link'] = FALSE;
			$config['prev_link'] = FALSE;
			
			//$config['use_page_numbers'] = FALSE;
			//$config['page_query_string'] = FALSE;
			
			$config['full_tag_open'] = '<ul class="pager">';
			$config['full_tag_close'] = '</ul>';
			
			$config['num_tag_open'] = '<li id="hallo">';
			$config['num_tag_close'] = '</li>';
			
			$config['cur_tag_open'] = '<li class="current">';
			$config['cur_tag_close'] = '</li>';
			
			$this->pagination->initialize($config);
			
			$hasil = $this->load->view('f-jc-sgs',$data,TRUE);
			$agent = "";
			if($data['tipe_penjualan'] <>''){
				$agent = "show";
			}
			$jml=count($data['getJourney']);
			if($agent <> '') $agent='show';
			if($data['bulan'] < date("m")){
				$agent = "";
			}else{
				$agent="show";
			}
			echo json_encode(array(
				'status'=>'OK',
				'jumlah'=>$jml,
				'datatabel' => $hasil,
				'agent' => $agent
			));
			return; 
		}
	}
	
	function get_jc_sgs_status(){
		$status 		= $this->input->post('status');
		
		$hasil = '<select name="sfa" id="sfa" class="d_jc_ds">';
		$hasil .= '<option value="">-Choice-</option>';
		
		if($status <>"" ){
			##ambil ds
			$getDS		= $this->mjourney->getDS($status);
			foreach($getDS as $r){
					$hasil .='<option value="'. $r->user_id .'">'. $r->user_name .'</option>';
			}
		}		
		$hasil .= '</select>';
		
		echo json_encode(array(
			'status'=>'OK',
			'hasil'=> $hasil
		));
		return; 
	}
	
	function add_agent_sgs(){
		$data['bulan'] 	= $this->input->post('bulan');
		$data['tahun'] 		= date('Y');
		$data['sfa'] 		= $this->input->post('sfa');
		$c_agent 		= $this->input->post("c_agent");
		
		if(count($c_agent) > 0 && $data['sfa'] <> "" && $data['bulan'] <>""){
			
			$this->mjourney->insertJourney_sgs($c_agent,$data['sfa'],$data['bulan'],date("Y"));
			$data['getJourney'] 	= $this->mjourney->getJourneySGS($data['sfa'],$data['bulan'],$data['tahun']);
			$data['getHoliday']	= $this->mjourney->getHoliday($data['bulan'],date("Y"));
			$hasil = $this->load->view('f-jc-sgs',$data,TRUE);
			echo json_encode(array(
				'status'=>'OK',
				'jumlah'=>count($data['getJourney']),
				'datatabel' => $hasil
			));
			return;
		}		
	}
	
	function toko_json($jenis){
	    echo($this->mjourney->getToko_json($jenis));
	}
	
	///////////////////////////////////////////
	///UPLOAD JC (MANUAL)//////////////////////
	///////////////////////////////////////////
	public function upload_jc() 
	{
		$data['template'] = "shell/smooth";
		$data['main_view'] = "f-upload-jc";
		$data['error'] ="";
		//$this->mjourney->createTabelJCManual();
		$this->load->view($data['template'], $data);
		
	}
	
	function upload(){
        $data['template'] = "shell/smooth";
		$data['main_view'] = "f-upload-jc";

		$data['error'] ="";
		if($_FILES['file_excel']['name'] <> "" ){
			$config['upload_path'] = './file/mfino/';
			//$config['file_name'] = $this->session->userdata('username');
			//$config['allowed_types'] = 'jpg|jpeg|JPG|JPEG|xls|xlsx|XLS|XLSX' ;
			$config['allowed_types'] = '*' ;
			$config['max_size'] = '2500000';
			$config['overwrite'] = TRUE;
			
			$this->load->library('upload', $config);
			$this->upload->initialize($config);
			
			if(!$this->upload->do_upload("file_excel")){
				$data['error'] = $_FILES['file_excel']['type'].$this->upload->display_errors();
			}else{
				//baca file excel di sini
				$info = $this->upload->data();
				
				//if( $_FILES['file_excel']['type'] == 'application/vnd.ms-excel'){ //versi 2003
				if($info['file_ext'] == '.xls'){
					$this->load->library('PHPExcel');
					$objReader = new PHPExcel_Reader_Excel5();
					$objReader->setReadDataOnly(true);
					$objPHPExcel = $objReader->load( $info['full_path'] );
					$rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
					$sheet = $objPHPExcel->getActiveSheet();
					$array_data = array();
					
					$i=1;
					//$this->db->trans_start(); 					// Save trans
					foreach($rowIterator as $row){
						if($i>1){
							$rowIndex = $row->getRowIndex ();
							$array_data[$rowIndex] = array('A'=>'', 'B'=>'','C'=>'','D'=>'');
						 
							$parent = $sheet->getCell('C' . $rowIndex);
							$channel = $sheet->getCell('E' . $rowIndex);
							
							$d1 = $sheet->getCell('I' . $rowIndex);
							$d2 = $sheet->getCell('J' . $rowIndex);
							$d3 = $sheet->getCell('K' . $rowIndex);
							$d4 = $sheet->getCell('L' . $rowIndex);
							$d5 = $sheet->getCell('M' . $rowIndex);
							$d6 = $sheet->getCell('N' . $rowIndex);
							$d7 = $sheet->getCell('O' . $rowIndex);
							$d8 = $sheet->getCell('P' . $rowIndex);
							$d9 = $sheet->getCell('Q' . $rowIndex);
							$d10 = $sheet->getCell('R' . $rowIndex);
							$d11 = $sheet->getCell('S' . $rowIndex);
							$d12 = $sheet->getCell('T' . $rowIndex);
							$d13 = $sheet->getCell('U' . $rowIndex);
							$d14 = $sheet->getCell('V' . $rowIndex);
							$d15 = $sheet->getCell('W' . $rowIndex);
							$d16 = $sheet->getCell('X' . $rowIndex);
							$d17 = $sheet->getCell('Y' . $rowIndex);
							$d18 = $sheet->getCell('Z' . $rowIndex);
							$d19 = $sheet->getCell('AA' . $rowIndex);
							$d20 = $sheet->getCell('AB' . $rowIndex);
							$d21 = $sheet->getCell('AC' . $rowIndex);
							$d22 = $sheet->getCell('AD' . $rowIndex);
							$d23 = $sheet->getCell('AE' . $rowIndex);
							$d24 = $sheet->getCell('AF' . $rowIndex);
							$d25 = $sheet->getCell('AG' . $rowIndex);
							$d26 = $sheet->getCell('AH' . $rowIndex);
							$d27 = $sheet->getCell('AI' . $rowIndex);
							$d28 = $sheet->getCell('AJ' . $rowIndex);
							$d29 = $sheet->getCell('AK' . $rowIndex);
							$d30 = $sheet->getCell('AL' . $rowIndex);
							$d31 = $sheet->getCell('AM' . $rowIndex);
							
							$idata = array('parent_id'=>$parent->getCalculatedValue(),
										   'channel_id'=>$channel->getCalculatedValue(),
										   'yyyymm'=>'201207');
							for($i=1;$i<=31;$i++){
								//echo "[".$i."=>".${"d".$i}->getCalculatedValue() ."]";								
								//if(${"d".$i}->getCalculatedValue()=="1" || ${"d".$i}->getCalculatedValue()=="v"){
								$val = ${"d".$i}->getCalculatedValue();
								if($val=="1"){
									$idata["d".$i] = 1;									
								} else 
								$idata["d".$i] = 0;
								//echo "[".$i."=>".$val."/".$idata["d".$i]."]";								
							}
							
							$user  = $this->mjourney->cekNIK($parent->getCalculatedValue());
							$agent = $this->mglobal->showdata('agent_id','t_mtr_agent',array('agent_id'=>$channel->getCalculatedValue()),'dblokal');
							$outlet= $this->mglobal->showdata('outlet_id','t_mtr_outlet',array('outlet_id'=>$channel->getCalculatedValue()),'dblokal');
							
							
							//if($parent->getCalculatedValue()<>"" && isset($parent->getCalculatedValue()) ){
								
							//}
							
							if($user <> 0){
								//**
								//echo "outlet=>".$outlet['outlet_id'] . "getNIK=>".$parent->getCalculatedValue();
								//**
								if( ($agent=="" && $outlet=="")) {
									$nv="";
									if($user==0) $nv .= "parent_id,";
									if($agent=="" && $outlet=="") $nv .= "channel_id";
									$idata["data_valid"]  = $nv;
									$this->mjourney->addjcmanual($idata);	// simpan ke database, data gak valid
								}else{
									//cek sudah ada tabelnya belum? => buat
									//cek sudah ada datanya blm? => abaikan
									
									//ambil nama parent-nya dulu
									$nmparent = $this->mjourney->getNIK($parent->getCalculatedValue());
									//echo $nmparent;
									$this->mjourney->addjcmanualsfa($idata,$parent->getCalculatedValue());	// simpan ke database
									//$this->mjourney->addjcmanualsfa($idata,$nmparent);	// simpan ke database
									
									
									/*
									if($outlet<>"" && $parent->getCalculatedValue() == "92001240"){ //langsung input ke tabel
										$this->mjourney->addjcmanualsfa($idata,$parent->getCalculatedValue());	// simpan ke database
									}else{
										$idata["data_valid"]  = "yes";
										$idata["jenis"] = $agent==""?"sfa":"ds";
										$this->mjourney->addjcmanual($idata);	// simpan ke database
									}
									*/
								}
							}
							
							//if ($this->db->trans_status() === FALSE)	// Save trans
							//	$this->db->trans_rollback();			// Save trans
							//else
							//	$this->db->trans_commit();				// Save trans
							
						}
						$i++;
					}
					
					
				}elseif(  $info['file_ext'] == '.xlsx' ){ //versi 2007
					
	/*				$this->load->library('PHPExcel');
					$objReader = new PHPExcel_Reader_Excel2007();
					$objReader->setReadDataOnly(true);
					$objPHPExcel = $objReader->load( $info['full_path'] );
					$rowIterator = $objPHPExcel->getActiveSheet()->getRowIterator();
					$sheet = $objPHPExcel->getActiveSheet();
					$array_data = array();
					foreach($rowIterator as $row){
						$rowIndex = $row->getRowIndex ();
						$array_data[$rowIndex] = array('A'=>'', 'B'=>'','C'=>'','D'=>'');
					 
						$cell = $sheet->getCell('A' . $rowIndex);
						$array_data[$rowIndex]['A'] = $cell->getCalculatedValue();
						$cell = $sheet->getCell('B' . $rowIndex);
						$array_data[$rowIndex]['B'] = $cell->getCalculatedValue();
						$cell = $sheet->getCell('C' . $rowIndex);
						$array_data[$rowIndex]['C'] = PHPExcel_Style_NumberFormat::toFormattedString($cell->getCalculatedValue(), 'YYYY-MM-DD');
						$cell = $sheet->getCell('D' . $rowIndex);
						$array_data[$rowIndex]['D'] = $cell->getCalculatedValue();
					}
					print_r($array_data);
	*/
				}
			}
		}
		$this->load->view($data['template'], $data);
		// echo '<script language="javascript" type="text/javascript">window.top.window.stopUpload();</script>  ';
    }
	
	
	
	////////////////////////////////
	////////////////////////////////
	function deletedobel(){
		$this->mjourney->deletedobel();
	}
}